// pages/newAddress/newAddress.js
import api from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    province:Object,
    // 省份数组
    arrProvince:Array,
    // 省份id
    arrProvince_id:2,
    // 市区数组
    arrCity:Array,
    // 市区ID
    arrCity_id:Number,
    // 区数组
    arrdistrict:Array,
    // 区id
    arrdistrict_id:Number,
    CityID:[],
    address:"",
    // 收货人
    person:"",
    phoneNumber:"",
    addressdetail:"",
     province_id:String,
     city_id: String,
     district_id: String,
     showQuyu:false,
    //  区域地址
    Location:'',
    changeA:[],
    is_default:false
  },
  // 收货人
  setPsesonData(e){
    console.log(e)
    this.setData({
      person: e.detail.value
    })
  },
  // 手机号码
  setPhoneData(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  // 详细地址
  setAddressData(e) {
    this.setData({
      addressdetail: e.detail.value
    })
  },
  addAddress(){
    this.Quyu();
  },
  Quyu(){
    let b = !this.data.showQuyu;
    this.setData({
      showQuyu:b
    })
  },
  // 获取省份
  getRegion(){
    wx.request({
      url: api.regionList,
      data:{
        parentId:1
      },
      success:res=>{
        console.log(res)
        let arrProvince=[];
        let province= res.data.data;
        province.forEach((el,index)=>{
          arrProvince.push(el.name)
        })
        this.setData({
          province,
          arrProvince
        });
      // 设置市区
        wx.request({
          url: api.regionList,
          data: {
            parentId: 2
          },
          success: res => {
            let arrCity = []
            console.log(res)
            let City = res.data.data;
            let CityID = []
            City.forEach((el, index) => {
              arrCity.push(el.name);
              CityID.push(el.id)
            })
            this.setData({
              arrCity,
              CityID
            });
          // 设置市区下面的每一个区、县
            wx.request({
              url: api.regionList,
              data: {
                parentId: 37
              },
              success: res => {
                let arrdistrict = [];
                let city = res.data.data;
                city.forEach((el, index) => {
                  arrdistrict.push(el.name)
                })
                this.setData({
                  arrdistrict
                });

              }
            })

          }
        })


      }
    })
  },
  // 当省份改变时，改变第二列的值
  changeAddress(e){

    console.log(e)
    this.setData({
      arrProvince_id: e.detail.value[0]+2
    })
    // console.log(this.data.arrProvince[e.detail.value[0]])
    console.log(e.detail.value[0] + 2)
    wx.request({
      url: api.regionList,
      data:{
        parentId: this.data.arrProvince_id
      },
      success:res=>{
        
        let arrCity=[]
        console.log(res)
        let City=res.data.data;
        let CityID = []
        City.forEach((el,index)=>{
          arrCity.push(el.name);
          CityID.push(el.id)
        })
        this.setData({
          arrCity,
          CityID
        });
        console.log(CityID)
        this.getArrdistrict(e.detail.value[1],e);
      }
    })
  },
  // 获取区
  getArrdistrict(id,e){
    console.log(this.data.CityID)
    let arrCity_id = this.data.CityID[id];
    console.log(arrCity_id)
    this.setData({
      arrCity_id
    })
    wx.request({
      url: api.regionList,
      data:{
        parentId: arrCity_id
      },
      success:res=>{
        console.log(res.data.data);
        let arrdistrict=[];
        let city = res.data.data;
        let cityId=[]
        city.forEach((el,index)=>{
          arrdistrict.push(el.name);
          cityId.push(el.id)
        });
        console.log(this.data.Location)
         
        this.setData({
          arrdistrict,
          arrdistrict_id: cityId[e.detail.value[2]]
        });
        let aa = this.data.arrProvince[e.detail.value[0]] +" "+ this.data.arrCity[e.detail.value[1]] +" "+ this.data.arrdistrict[e.detail.value[2]];
        console.log(this.data.arrCity_id);
        console.log(this.data.arrProvince_id);
        console.log(this.data.arrdistrict_id)
        // let province_id=String,
        // city_id: String,
        // district_id: String,

        this.setData({
          Location: aa
        })
       
      }
    })
  },
  // 保存按钮
  keepAddress(){
    console.log(this.data.person)
    console.log(this.data.phoneNumber)
    console.log(this.data.arrProvince_id)
    console.log(this.data.arrCity_id)
    console.log(this.data.arrdistrict_id)
    console.log(this.data.addressdetail)
    console.log(this.data.is_default)
    if(this.data.id){
      wx.request({
        url: api.saveAddress,
        data: {
          id:this.options.id,
          name: this.data.person,
          mobile: this.data.phoneNumber,
          province_id: this.data.arrProvince_id,
          city_id: this.data.arrCity_id,
          district_id: this.data.arrdistrict_id,
          address: this.data.addressdetail,
          is_default: this.data.is_default
        },
        header: {
          'X-Nideshop-Token': wx.getStorageSync("token").token
        },
        method: "POST",
        success: res => {
          console.log(res)
        }
      })
    }
    else{
      console.log('添加地址')
      wx.request({
        url: api.saveAddress,
        data: {
          name: this.data.person,
          mobile: this.data.phoneNumber,
          province_id: this.data.arrProvince_id,
          city_id: this.data.arrCity_id,
          district_id: this.data.arrdistrict_id,
          address: this.data.addressdetail,
          is_default: this.data.is_default
        },
        header: {
          'X-Nideshop-Token': wx.getStorageSync("token").token
        },
        method: "POST",
        success: res => {
          console.log(res)
        }
      })
    }
    wx.navigateBack({
      delta:1
    })
    
  },
  deleteAddress(){
    wx.request({
      url: api.deleteAddress,
      data:{
        id:this.options.id
      },
      header: {
        'X-Nideshop-Token': wx.getStorageSync("token").token
      },
      method:"POST",
      success:res=>{
        console.log(res)
        wx.reLaunch({
          url: '../receivingAddress/receivingAddress',
        })
      }
    })
  },
  // 默认地址
  defaultAddress(e){
    console.log(e)
    if (e.detail.value){
      this.setData({
       is_default:true
      })
    }
    else{
      is_default: false
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRegion();
    console.log(this)
    // console.log(Boolean(this.options.id))
    if (Boolean(this.options.id)){
      wx.request({
        url: api.changeAddress,
        data:{
          id: this.options.id
        },
        header: {
          'X-Nideshop-Token': wx.getStorageSync("token").token
        },
        success:res=>{
          let id=this.options.id;
          console.log(res);
          this.setData({
            changeA:res.data.data,
            person:res.data.data.name,
            phoneNumber: res.data.data.mobile,
            arrProvince_id: res.data.data.province_id,
            arrCity_id: res.data.data.city_id,
            arrdistrict_id: res.data.data.district_id,
            addressdetail:res.data.data.address,
            Location:res.data.data.full_region,
            addressdetail: res.data.data.address,
            id
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})